package lib;

import java.util.ArrayList;
import java.util.List;

import lib.api.bierdopje.BierdopjeException;
import lib.api.bierdopje.JBierdopjeApi;
import lib.api.bierdopje.JBierdopjeRSS;
import lib.api.bierdopje.model.JBierEpisode;
import lib.api.bierdopje.model.JBierShow;
import lib.api.bierdopje.model.JBierSubtitle;
import lib.logging.Logger;
import lib.model.EpisodeFile;
import lib.model.MovieFile;
import lib.model.Subtitle;
import lib.model.SubtitleMatchType;
import settings.model.MappingBierdopjeScene;

public class JBierSubAdapter implements JSubAdapter{

    private final JBierdopjeApi jbapi;
    private final JBierdopjeRSS jbrss;

    public JBierSubAdapter(){
        //this.jbapi = new JBierdopjeApi("D00BBE85B1C00F40");
        this.jbapi = new JBierdopjeApi("F14628EABA5A7EBD");
        jbrss = new JBierdopjeRSS();
    }

    private String checkMappingBierdopjeScene(String showname, List<MappingBierdopjeScene> dict) {
    	Logger.instance.trace("JBierSubAdapter", "checkMappingBierdopjeScene", "");
        for (MappingBierdopjeScene aDict : dict) {
            if (showname.equalsIgnoreCase(aDict.getSceneName())) {
            	Logger.instance.debug("Found BierdopjeName in mapping: " + aDict.getBierdopjeName());
                return aDict.getBierdopjeName();
            }
        }
        return showname;
    }
    
    private int checkMappingBierdopjeIDScene(String showname, List<MappingBierdopjeScene> dict){
    	Logger.instance.trace("JBierSubAdapter", "checkMappingBierdopjeIDScene", "");
    	for (MappingBierdopjeScene aDict : dict) {
            if (showname.equalsIgnoreCase(aDict.getSceneName())) {
            	Logger.instance.debug("Found BierdopjeId in mapping: " + aDict.getBierdopjeId());
                return aDict.getBierdopjeId();
            }
        }
        return 0;
    }
    
    @Override
    public List<Subtitle> searchSubtitles(EpisodeFile episode, String... sublanguageids) {
    	Logger.instance.trace("JBierSubAdapter", "searchSubtitles", "");
        List<Subtitle> listFoundSubtitles = new ArrayList<Subtitle>();
        List<JBierSubtitle> lSubtitles = new ArrayList<JBierSubtitle>();
        try {
            lSubtitles = jbapi.GetAllSubsForEpisode(episode.getBierEpisode().getEpisodeid(), sublanguageids[0]);
        } catch (Exception e) {
            Logger.instance.error("API BIERDOPJE searchSubtitles " + e.getMessage());
        }
        for (JBierSubtitle bierSubtitle : lSubtitles) {
            listFoundSubtitles.add(new Subtitle(Subtitle.SubtitleSource.BIERDOPJE,
                    bierSubtitle.getFilename(),
                    bierSubtitle.getDownloadlink(),
                    sublanguageids[0],
                    "",
                    SubtitleMatchType.EVERYTHING));
        }
        return listFoundSubtitles;
    }
    
    public List<Subtitle> searchSubtitlesSeason(EpisodeFile episode, String languagecode) {
    	Logger.instance.trace("JBierSubAdapter", "searchSubtitlesSeason", "");
        List<Subtitle> listFoundSubtitles = new ArrayList<Subtitle>();
        List<JBierSubtitle> lSubtitles = new ArrayList<JBierSubtitle>();
        try {
            lSubtitles = jbapi.GetSubsForSeason(episode.getBierShow().getShowid(),
            		episode.getSeason(), languagecode);
        } catch (Exception e) {
            Logger.instance.error("API BIERDOPJE searchSubtitlesSeason " + e.getMessage());
        }
        for (JBierSubtitle bierSubtitle : lSubtitles) {
            listFoundSubtitles.add(new Subtitle(Subtitle.SubtitleSource.BIERDOPJE,
                    bierSubtitle.getFilename(),
                    bierSubtitle.getDownloadlink(),
                    languagecode,
                    "",
                    SubtitleMatchType.EVERYTHING));
        }
        return listFoundSubtitles;
    }

    public JBierEpisode searchEpisode(EpisodeFile episodeFile) {
    	Logger.instance.trace("JBierSubAdapter", "searchEpisode", "");
        List<JBierEpisode> lbepisode;

        try {
            lbepisode = jbapi.GetEpisodesForSeason(
            		episodeFile.getBierShow().getShowid(),
            		episodeFile.getSeason());
            
            for (JBierEpisode aLbepisode : lbepisode) {
                for (int epno : episodeFile.getEpisodeNumbers()) {
                    if (epno == aLbepisode.getEpisode()) {
                        return aLbepisode;
                    }
                }
            }
        } catch (BierdopjeException e) {
            Logger.instance.error("API BIERDOPJE searchEpisode " + e.getMessage());
        }

        return null;
    }

    public JBierShow searchShow(EpisodeFile newEp, List<MappingBierdopjeScene> dict) {
    	Logger.instance.trace("JBierSubAdapter", "searchShow", "");
    	return this.searchShow(newEp.getShow(), dict);
    }

    public JBierShow searchShow(String show, List<MappingBierdopjeScene> dict) {
    	Logger.instance.trace("JBierSubAdapter", "searchShow", "");
        try {
            int showid = this.checkMappingBierdopjeIDScene(show, dict);
            if (showid > 0)
                return jbapi.GetShowById(showid);
            String showname = checkMappingBierdopjeScene(show, dict);
            return jbapi.FindShowByName(showname);
        } catch (BierdopjeException e) {
            Logger.instance.error("API BIERDOPJE searchShow " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

	public List<Subtitle> getRssSubs(EpisodeFile episode, String languagecode) {
		Logger.instance.trace("JBierSubAdapter", "getRssSubs", "");
		List<Subtitle> listFoundSubtitles = new ArrayList<Subtitle>();
        List<JBierSubtitle> lSubtitles = new ArrayList<JBierSubtitle>();
        try {
			lSubtitles = jbrss.searchRSS(episode.getFilename(),
						episode.getBierShow().getShowid(),
			    		episode.getShow(),
			    		episode.getSeason(),
			    		episode.getEpisodeNumbers(), languagecode);
		} catch (Exception e) {
			Logger.instance.error("API BIERDOPJE getRssSubs " + e.getMessage());
		}
        
        for (JBierSubtitle bierSubtitle : lSubtitles) {
            listFoundSubtitles.add(new Subtitle(Subtitle.SubtitleSource.BIERDOPJE,
                    bierSubtitle.getFilename(),
                    bierSubtitle.getDownloadlink(),
                    languagecode,
                    "",
                    SubtitleMatchType.EVERYTHING));
        }
        return listFoundSubtitles;
	}

    @Override
    public List<Subtitle> searchSubtitles(MovieFile movieFile,
            String... sublanguageids) {
        return new ArrayList<Subtitle>();
    }

}
